package pdm.yamba.service;

import pdm.yamba.dto.User;
import pdm.yamba.feed.TwitterFeed;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

public class UserInfoPullService extends Service {

	private Handler _uiHandler;
	
	@Override
	public void onCreate() 
	{
		super.onCreate();
		_uiHandler = new Handler();
		Toast.makeText(this, "onCreate()", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onDestroy() 
	{
		Toast.makeText(this, "onDestroy()", Toast.LENGTH_LONG).show();
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) 
	{
		return new IUserInfoPullService.Stub() {
			
			@Override
			public User GetUserInfo(String username, String password, String serviceUrl) throws RemoteException 
			{
				//TODO
				
				
				_uiHandler.post(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(UserInfoPullService.this, "UserInfo Done", Toast.LENGTH_LONG).show();
					}
				});

				TwitterFeed twitterFeed = new TwitterFeed(username, password, serviceUrl);
				
				winterwell.jtwitter.Twitter.User twiteerUser = twitterFeed.GetUserInfo(username);
				
				User user = new User(username, password, true);
				user.setMessageCount(twiteerUser.statusesCount);
				user.setSubscribersCount(twiteerUser.followersCount);
				user.setSubscriptionNumber(twiteerUser.friendsCount);
				
				return user;
			}
		};
	}

	@Override
	public boolean onUnbind(Intent intent) 
	{
		Toast.makeText(this, "onUnbind()", Toast.LENGTH_LONG).show();
		return super.onUnbind(intent);
	}

	@Override
	public void onRebind(Intent intent) 
	{
		Toast.makeText(this, "onRebind()", Toast.LENGTH_LONG).show();
		super.onRebind(intent);
	}

}